//
//  LNMainViewController.swift
//  727-黑马08微博-测试
//
//  Created by apple on 15/7/28.
//  Copyright © 2015年 itheima. All rights reserved.
//

import UIKit

class LNMainViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        addChildViewControllers()
//        此时tabBarButton还没有创建
        
        print(tabBar.subviews)
        
    }

    override func viewDidAppear(animated: Bool) {
         super.viewDidAppear(animated)
//        此时tabBarButton都已经创建
    setupComposeButton()
        
}
//    运行循环监听到事件后，向VC发送消息，动态执行方法，因此不能设置为private
    func clickComposedButton() {
//    根据用户是否登录，来决定是否能够显示发布微博的界面
        let vc = LNUserAccount.userLogon ? LNComposeViewController() : LNOAuthViewController()
        let nav = UINavigationController(rootViewController: vc)
        presentViewController(nav, animated: true, completion: nil)
}


// 设置撰写按钮位置
    private func setupComposeButton() {
    let w = tabBar.bounds.width / CGFloat(viewControllers!.count)
let rect = CGRect(x: 0, y: 0, width: w, height: tabBar.bounds.height)
composeButton.frame  = CGRectOffset(rect, 2*w, 0)
}
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
//    添加所有控制器
    private func addChildViewControllers() {
//        只是添加控制器，并没有创建tabBar的Button
        addChildViewController(LNHomeTableViewController(), title:"首页", imageName: "tabbar_home")
        addChildViewController(LNMessageTableViewController(), title:"消息", imageName: "tabbar_message_center")
        addChildViewController(UIViewController())
        addChildViewController(LNDiscoverTableViewController(), title: "发现", imageName: "tabbar_discover")
        addChildViewController(LNProfileTableViewController(), title: "我", imageName: "tabbar_profile")
        
    }
    
// 添加子控件
    private func addChildViewController(vc: UIViewController, title:String, imageName: String) {
//        设置tintColor
        tabBar.tintColor = UIColor.orangeColor()
//        title属性会从内向外传递设置
        vc.title = title
        vc.tabBarItem.image = UIImage(named: imageName)
        let nav = UINavigationController(rootViewController: vc)
        addChildViewController(nav)
    }

//懒加载
    lazy private var composeButton: UIButton = {
        // 自定义类型的按钮
        let button = UIButton()
        
        button.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        button.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        
        self.tabBar.addSubview(button)
        button.addTarget(self, action: "clickComposedButton", forControlEvents: UIControlEvents.TouchUpInside)
        
        return button
        }()


}


























